<%@page import="java.awt.print.Printable"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<meta HTTP-EQUIV="pragma" CONTENT="no-cache">
<meta HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<meta HTTP-EQUIV="expires" CONTENT="0">
<title>_${customer.customerName}_灯光架管理</title>
<script type="text/javascript">
  
  $(function() {
		$("#queryForm").validate(); 
		//$("#_customerCode").trigger('change');
  });

  /**
   * 全角符号，转半角符号
   */
	function getString(str){  
		var result = "";   
		for (var i = 0; i < str.length; i++) {  
			if (str.charCodeAt(i) == 12288){
			     result += String.fromCharCode(32); 
			}else if (str.charCodeAt(i) > 65280 && str.charCodeAt(i) < 65375){  
			    result += String.fromCharCode(str.charCodeAt(i) - 65248); 
			}else{  
			   result += String.fromCharCode(str.charCodeAt(i));
			}
		}
		return result;  
	} 
  
	/**
	 * 新增灯光架，参数检测
	 */
	function checkBoardCode(boardCode) {
		var reg = new RegExp('[0-9],[0-9]*,[0-9]');
		return reg.test(boardCode);
	}
	
	//创建灯光架
	function createLightShelf() {
		$("#btn_submit").attr("disabled", true);

		if ($("#inputForm").valid()) {
			var shelfName = $("#shelfName").val().trim();
			var shelfType = $("#shelfType").val().trim();
			var boardCode = $("#boardCode").val().trim();
			var customerCode = $("#customerCode").val().trim();
			var warehouseCode = $("#warehouseCode").val().trim();
			
			boardCode = getString(boardCode);
			if(!checkBoardCode(boardCode)){
				$("#boardCode").focus();
				$("#prompt").show().fadeOut(3000);
				$("#btn_submit").attr("disabled", false);
				return;
			}

			$.ajax({
				type : 'post',
				url : '${ctx}/lightShelf/ajaxCreateLightShelf',
				data : {
					shelfName : shelfName,
					shelfType : shelfType,
					boardCode : boardCode,
					customerCode : customerCode,
					warehouseCode : warehouseCode
				},
				dataType : 'json',
				success : function(data) {
					if (data == true) {
						$("#queryForm").submit();
					} else {
						alert("网络繁忙，请稍后重试...");
					}
					$("#btn_submit").attr("disabled", false);

				},
				error : function() {
					alert("网络繁忙，请稍后重试...");
					$("#btn_submit").attr("disabled", false);
				}
			});

		} else {
			$("#btn_submit").attr("disabled", false);
		}
	}

	//重设表单
	function formReset() {
		$("input[type=reset]").trigger("click");
	}

	//打开创建灯光架窗体
	function openWin() {
		if ($("#queryForm").valid()) {
			formReset();
			$("#btn_submit").attr("disabled", null);
			$("#inputForm")
					.attr("action", '${ctx}/lightShelf/createLightShelf');
			$("#warehouseCode").val($("#_warehouseCode").val());
			$("#customerCode").val($("#_customerCode").val());
			$("#newLightShelf").trigger("click");

		}
	}

	//删除
	function ajaxDeleteLightShelf(lightShelfId) {

		$.ajax({
			type : 'post',
			url : '${ctx}/lightShelf/ajaxDeleteLightShelf',
			data : {
				lightShelfId : lightShelfId
			},
			dataType : 'json',
			success : function(data) {
				if (data == true) {
					$("#queryForm").submit();
				} else {
					alert("网络繁忙，请稍后重试...");
				}

			},
			error : function() {
				alert("网络繁忙，请稍后重试...");
			}
		});
	}

	//释放
	function ajaxFree(lightShelfId) {
		$.ajax({
			type : 'post',
			url : '${ctx}/lightShelf/ajaxFree',
			data : {
				lightShelfId : lightShelfId
			},
			dataType : 'json',
			success : function(data) {
				if (data == true) {
					$("#queryForm").submit();
				} else {
					alert("网络繁忙，请稍后重试...");
				}
			},
			error : function() {
				alert("网络繁忙，请稍后重试...");
			}
		});
	}

	//加载灯光架详细信息
	function ajaxLoadLightShelfDetail(lightShelfId) {
		$("#stockInfo").html("");
		$.ajax({
			type : 'post',
			url : '${ctx}/lightShelf/ajaxLoadLightShelfDetail',
			data : {
				lightShelfId : lightShelfId
			},
			dataType : 'json',
			success : function(data) {

				var shelf = data.lightShelf;
				var stockList = data.stockList;

				$("#_shelfName").html(shelf["shelfName"]);
				$("#_guige").html(shelf["lineNum"] + "x" + shelf["rowNum"]);
				$("#_stockNum").html(shelf["stockNum"]);
				$("#_batchCode").html(shelf["batchCode"]);
				$("#_taskCode").html(shelf["taskCode"]);
				var status = shelf["status"];
				if (status == 'busy') {
					$("#_status").html("<font color=red>使用中</font>");
				} else if (status == 'free') {
					$("#_status").html("闲置中");
				}

				var str = "";
				$.each(stockList, function(i) {
					var stock = stockList[i];
					str += "<tr>"
					str += "<td>" + (i + 1) + "</td>";
					str += "<td>" + stock.stockName + "</td>";
					str += "<td>" + stock.stockCode + "</td>";
					str += "<td>" + stock.orderno + "</td>";
					str += "<td>" + stock.goodsNum + "</td>";
					str += "<td>" + stock.goodsInfo + "</td>";

					var status = stock.status;
					if (status == 'busy') {
						str += "<td><font color=red>使用中</font></td>";
					} else if (status == 'free') {
						str += "<td>闲置中</td>";
					}

					str += "</tr>";
				});
				$("#stockInfo").html(str);

			},
			error : function() {
				alert("网络繁忙，请稍后重试...");
			}
		});
	}

	//init打印机
	function initPrinter(item) {
		//var indexLength = getPrinterCount();
		//var tempString = "";
		//for ( var i = 0; i < indexLength; i++) {
		//	var name = getPrinterName(i);
		//	tempString += "<option value='"+name+"'>" + name + "</option>";
		//}
		$("#printerNumber").html("打印编码(<font color=blue>" + item + "</font>)");
		$("#hiddenCode").val(item);
		//$("#printerBody").html(tempString);
		packFormPrinter("printerBody");
	}

	//打印
	function clickPrinter() {
		var code = $("#hiddenCode").val();
		var printName = $("#printerBody").val();
		printBarCode(printName, code);
	}

	function printBarCode(printName, taskName) {
		LODOP = getLodop(document.getElementById('LODOP_OB'), document
				.getElementById('LODOP_EM'));
		LODOP.PRINT_INIT(taskName);
		LODOP.SET_PRINT_PAGESIZE(1, 600, 320, "");
		LODOP.ADD_PRINT_BARCODE(25, 10, 200, 45, "128B", taskName);
		LODOP.SET_PRINT_STYLE("FontName", "SimHei");
		LODOP.SET_PRINT_STYLE("FontSize", 25);
		LODOP.ADD_PRINT_TEXT(70, 30, 200, 100, taskName);
		if (LODOP.SET_PRINTER_INDEXA(printName)) {
			LODOP.PRINT();
		}
	}
</script>
</head>

<body>
<section class="content">
	<div class="row">
		<div class="col-md-12">

			<!-- Primary box -->
			<div class="box box-solid box-primary">
				<div class="box-header">
					<i class="fa fa-search"></i>
					<h3 class="box-title">搜索栏</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
				<div class="box-body">
		     <form class="form-search" id="queryForm" method="post" action="${ctx}/lightShelf/v2_queryLightShelf" >
			 	<div class="form-group row">
						<div class="col-sm-3">
							<div class="input-group col-sm-12">
			 	<label class="input-group-text">商家:</label>
				 <select  name="_customerCode"  id="_customerCode" class="form-control required" onchange="ajaxAreaGroup(this,'_warehouseCode');">
					<option value=""></option>
					<c:forEach items="${customerList}" var="customer">
						<option value="${customer.customerCode}"
						<c:if test="${param._customerCode eq customer.customerCode}">selected=selected</c:if>
						>${customer.customerName}</option>
					</c:forEach>
				</select>
			 	</div>
			 	</div>
			 	
			 	<div class="col-sm-3">
							<div class="input-group col-sm-12">
				<label class="input-group-text" >仓库:</label>
				 <select  name="_warehouseCode"  id="_warehouseCode"  class="form-control required" >
				 	<option value=""></option>
				 	<c:forEach items="${warehouseList}" var="warehouse">
						<option value="${warehouse.groupCode}" <c:if test="${param._warehouseCode eq warehouse.groupCode}">selected=selected</c:if>
						>${warehouse.groupName}</option>
					</c:forEach>
				</select>
				</div>
				</div>
				
				<div class="col-sm-4">
							<div class="input-group col-sm-12">
			    <button class="btn btn-info" type="submit" >搜索</button>
			    <a  data-toggle="modal" role="button"  class="btn btn-info" onclick="openWin();" >添加灯光架</a>
			    <a href="#win_newLightShelf" role="button" id="newLightShelf" data-toggle="modal" style="display: none;"></a>
			    </div>
			    </div>
			    
			    </div>
		    </form>
		    
	</div>
	</div>
	</div>
	</div>
	
	<div class="form-group row">
		<div class="col-md-12">
			<div class="box box-solid box-primary">
				<div class="box-header">
					<i class="fa fa-list"></i>
					<h3 class="box-title">查询结果</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
				<div class="box-body">
	<table id="contentTable" class="table table-striped table-bordered table-condensed">
		<thead><tr>					
			    <th style="width: 50px;">序号</th>
			    <th>商家</th>
			    <th>仓库</th>
			    <th>架名称</th>
			    <th>架编号</th>
			    <th>规格</th>
			    <th>库位数</th>
			    <th>状态</th>
			    <th>操作</th>
			</tr></thead>
		<tbody>
		<c:forEach items="${lightShelfList}" var="lightShelf" varStatus="i">
			<tr>
			   <td>${i.count}</td>
			   <td>${lightShelf.customer.customerName}</td>
			   <td>${lightShelf.warehouse.groupName}</td>
			   <td>${lightShelf.shelfName}</td>
			   <td>${lightShelf.shelfCode}</td>
			   <td>${lightShelf.lineNum}X${lightShelf.rowNum}</td>
			   <td>${lightShelf.stockNum}</td>
			   <td >
			     <c:if test="${lightShelf.status eq 'free'}">空置中</c:if>
			     <c:if test="${lightShelf.status eq 'busy'}"><font color=red>使用中</font></c:if>
			   </td>
			   <td>
			       <c:if test="${lightShelf.status eq 'busy'}">
			          <button  class="btn btn-danger" style="display: none;" onclick="ajaxFree('${lightShelf.id}');">释放</button>
			       </c:if> 
			       
			       <c:if test="${lightShelf.status eq 'free'}">
			          <button  class="btn btn-primary" onclick="ajaxDeleteLightShelf('${lightShelf.id}');">删除</button>
			       </c:if> 
			   
			        <a href="#win_detail" data-toggle="modal" role="button" onclick="ajaxLoadLightShelfDetail('${lightShelf.id}');"  class="btn btn-primary" >查看详情</a>
			         <a href="#selextPrinter" role="button" data-toggle="modal" class="btn btn-primary" onclick="initPrinter('${lightShelf.shelfCode}')">打印条码</a>
			   </td>
			</tr>
		</c:forEach>
		</tbody>
	</table>
	</div>
	</div>
	</div>
	</div>
	
	<!--灯光架  窗体 --> 
	<div  class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" id="win_newLightShelf" >
		<div class="modal-dialog" style="width:700px; height:250px;">
			<div class="modal-content">
		<div class="modal-header">
			<h4>创建灯光架</h4>
		</div>
		
		<div class="modal-body" style="width:680px;">
		<form id="inputForm"  method="post" class="form-horizontal">
		   <fieldset>
				  <!-- 行1 -->
			      <div class="form-group row">
						<div class="col-sm-6">
							<div class="input-group col-sm-12">
								<label  class="input-group-text">架名称:</label>
							    <input type="text" id="shelfName" name="shelfName" class="form-control required" >
							    <input type="hidden" id="customerCode" name="customerCode" >
							    <input type="hidden" id="warehouseCode" name="warehouseCode" >
							    <input id="btn_reset" type="reset" style="display:none;" /> 
			          </div>
			          </div>
			          <div class="col-sm-6">
							<div class="input-group col-sm-12">
								<label  class="input-group-text">类型:</label>
								    <select  name="shelfType"  id="shelfType" value="${param._warehouseCode}"  class="form-control required" >
								    	<option value="1">大架(5灯90位)</option>
								    	<option value="2">小架(10灯180位</option>
								    	<option value="3">小架大用(10灯90位)</option>
								    </select>
							</div>
				     </div>
			      </div>
			        <!-- 行2 -->
				   <div class="form-group row">
						<div class="col-sm-6">
							<div class="input-group col-sm-12">
								<label  class="input-group-text">板号:</label>
								<input type="text" id="boardCode" name="boardCode" class="form-control required">
							</div>
			          </div>
			          <div class="col-sm-6" >
							<div class="input-group col-sm-12">
								<font color="red">板号以逗号分隔,示例：01,02,03</font>
							</div>
							<div id="prompt" class="input-group col-sm-12" style="display:none;">
								<font color="red">板号格式不正确，参见示例。</font>
							</div>
				     </div>
			       </div>   
				       
			</fieldset>	 
			</form>
		</div>
		
		<div class="modal-footer bg-gray">
		    <button id="btn_submit" class="btn btn-primary" onclick="createLightShelf();">确定创建</button>&nbsp;	 
			<button id="btn_cancel" class="btn btn-primary" data-dismiss="modal" aria-hidden="true">返回</button>
		</div>
	</div>
	</div>
	</div>
	<!--详情 窗体 --> 
	<div  class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" id="win_detail" >
		<div class="modal-dialog" style="width:830px; height:500px;">
			<div class="modal-content">
		<div class="modal-header">
		</div>
		
		<div class="modal-body" style="width:830px; height:400px; overflow: auto">
		  
		   <!-- 灯光架信息 -->
		   <table id="contentTable" class="table table-striped table-bordered table-condensed">
		        <thead>
					<th style="vertical-align: middle; color: white;" colspan="11" bgcolor="#8EB4CB">灯光架信息</th>
				</thead>
				<thead><tr>					
					    <th>架名称</th>
					    <th>架编号</th>
					    <th>规格</th>
					    <th>库位数</th>
					    <th>作业批次号</th>
					    <th>作业任务号</th>
					    <th>架状态</th>
					</tr></thead>
					<tr>
					   <td id="_shelfName"></td>
					   <td id="_shelfCode"></td>
					   <td id="_guige"></td>
					   <td id="_stockNum"></td>
					   <td id="_batchCode"></td>
					   <td id="_taskCode"></td>
					   <td id="_status"></td>
					</tr>
				</tbody>
			</table>
		   
		   <!-- 架位信息 -->
		   <table id="contentTable" class="table table-striped table-bordered table-condensed">
		        <thead>
					<th style="vertical-align: middle; color: white;" colspan="11" bgcolor="#8EB4CB">架位详情</th>
				</thead>
				<thead><tr>					
					    <th style="width: 50px;">序号</th>
					    <th>架位名称</th>
					    <th>架位编号</th>
					    <th>订单号</th>
					    <th>商品数量</th>
					    <th>商品信息</th>
					    <th>架位状态</th>
					</tr></thead>
				<tbody id="stockInfo" style="height: 200px;">
				</tbody>
			</table>
		
		</div>
		
		<div class="modal-footer">
			<button id="btn_cancel" class="btn" data-dismiss="modal" aria-hidden="true">确定</button>
		</div>
	</div>
	</div>
	</div>
	
	<!-- 打印条码窗体 -->
	<div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" id="selextPrinter">
		 <div class="modal-dialog" style="width: 1000">
			<div class="modal-content">
		 <div class="modal-header">
			  <h3 id="printerNumber">打印条码</h3>
		  </div>
			
		  <div class="modal-body" style="margin-left: 100px;">
			  <select id="printerBody" name="printerBody" style="width: 300px;"></select>
			  <input type="hidden" id="hiddenCode" name="hiddenCode" >
		  </div>
			
		  <div class="modal-footer">
			  <button class="btn" data-dismiss="modal" aria-hidden="true" id="printer" onclick="clickPrinter()">开始打印</button>
			  <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
		  </div>
			</div>
			</div>
	</div>
</section>
</body>
</html>
